package xyz.syyrjx.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import xyz.syyrjx.entity.ResultInfo;
import xyz.syyrjx.entity.User;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Mr.Wan
 * @date 2022/10/11 - 21:31
 */
@RestController
public class RedisTokenController {
    private static final Jedis jedis = new Jedis("192.168.188.130",6379);

    @RequestMapping("/token/set")
    public void setToken(@RequestParam("token") String token,@RequestBody User user){
        jedis.setex(token, 60 * 60 * 24, JSON.toJSONString(user));
    }

    @RequestMapping("/token/get")
    public ResultInfo<User> getUser(@RequestBody String token){
        try {
            User u = JSON.parseObject(jedis.get(token),User.class);
            return ResultInfo.success(u);
        }catch (Exception e){
            return ResultInfo.fail(e.getMessage());
        }
    }
}
